package com.seleniti.enpareja.web.appbase.client.control.navigation;

import org.ioc.commons.impl.gwt.client.flowcontrol.activitymapper.GoPlace;
import org.ioc.commons.impl.gwt.client.flowcontrol.activitymapper.InvalidPlaceException;
import org.ioc.commons.utils.Format;

import com.google.gwt.core.client.GWT;
import com.google.gwt.place.shared.PlaceTokenizer;
import com.google.gwt.place.shared.Prefix;
import com.seleniti.enpareja.controller.control.place.EnParejaPlace;

public class SettingsPlace extends GoPlace<EnParejaPlace> {

	@Prefix("settings")
	public static class Tokenizer implements PlaceTokenizer<SettingsPlace> {

		@Override
		public SettingsPlace getPlace(String token) {
			try {
				String[] tokenParts = token.split(":");

				while (tokenParts[0].endsWith("/") && !tokenParts[0].isEmpty()) {
					tokenParts[0] = tokenParts[0].substring(0, tokenParts[0].length() - 1);
				}

				EnParejaPlace place = Enum.valueOf(EnParejaPlace.class, tokenParts[0]);
				String subPlaceToken = (tokenParts.length > 1) ? tokenParts[1] : null;

				String passwordChangeEnabledStr = (tokenParts.length > 2) ? tokenParts[2] : null;
				String showDownloadAppWhenPasswordStr = (tokenParts.length > 3) ? tokenParts[3] : null;

				boolean passwordChangeEnabled;
				try {
					passwordChangeEnabled = Boolean.valueOf(passwordChangeEnabledStr);
				} catch (Exception e) {
					passwordChangeEnabled = false;
				}
				boolean showDownloadAppWhenPassword;
				try {
					showDownloadAppWhenPassword = Boolean.valueOf(showDownloadAppWhenPasswordStr);
				} catch (Exception e) {
					showDownloadAppWhenPassword = false;
				}

				/**
				 * If we are going back from a view, we don't want to reset the
				 * view data
				 */
				return new SettingsPlace(place, false, subPlaceToken, passwordChangeEnabled, showDownloadAppWhenPassword);
			} catch (Exception e) {
				/**
				 * We do not want to stop the app flow, just to warn to the user
				 * the place was not found. We delegate that to the GWT
				 * exception controller.
				 */
				GWT.getUncaughtExceptionHandler().onUncaughtException(
						new InvalidPlaceException(Format.substitute("The place '{0}' is not valid.", token), e));

				return null;
			}
		}

		@Override
		public String getToken(SettingsPlace place) {
			StringBuffer token = new StringBuffer(EnParejaPlace.UserSettings.toString());

			token.append(':').append(place.getSubPlaceToken()!=null ? place.getSubPlaceToken() : "");
			token.append(':').append(((SettingsPlace) place).isPasswordChangeEnabled());
			token.append(':').append(((SettingsPlace) place).isShowDownloadAppWhenPassword());

			return token.toString();
		}
	}

	private boolean passwordChangeEnabled = true;
	private boolean showDownloadAppWhenPassword;

	public SettingsPlace(EnParejaPlace place, boolean resetView, String subPlaceToken) {
		super(place, resetView, subPlaceToken);
	}

	public SettingsPlace(EnParejaPlace place, boolean resetView, String subPlaceToken, boolean passwordChangeEnabled,
			boolean showDownloadAppWhenPassword) {
		this(place, resetView, subPlaceToken);
		this.passwordChangeEnabled = passwordChangeEnabled;
		this.showDownloadAppWhenPassword = showDownloadAppWhenPassword;
	}

	public SettingsPlace(GoPlace<EnParejaPlace> goPlace, boolean passwordChangeEnabled,
			boolean showDownloadAppWhenPassword) {
		this(goPlace.getPlaceId(), goPlace.isResetView(), goPlace.getSubPlaceToken());
		this.passwordChangeEnabled = passwordChangeEnabled;
		this.showDownloadAppWhenPassword = showDownloadAppWhenPassword;
	}

	public boolean isPasswordChangeEnabled() {
		return passwordChangeEnabled;
	}

	public boolean isShowDownloadAppWhenPassword() {
		return showDownloadAppWhenPassword;
	}

}
